import {Component} from '@angular/core';
import {AlertController, IonicPage, NavController, NavParams} from 'ionic-angular';

/**
 * 变更房间页面
 * @Author AileYoung on 2018/1/19
 */

@IonicPage()
@Component({
  selector: 'page-change-table',
  templateUrl: 'change-table.html',
})
export class ChangeTablePage {
  private callBack;
  private changeRoomInfo= {
    roomType: {
      label: '所选房型',
      name: 'roomType',
      val: ''
    },
    roomFloor: {
      label: '所选楼层',
      name: 'roomFloor',
      val: ''
    },
    roomNumber: {
      label: '所选房间',
      name: 'roomNumber',
      val: ''
    },
    roomCustomer: {
      label: '入住人',
      name: 'roomCustomer',
      val: ''
    },
    endTime: {
      label: '离店时间',
      name: 'endTime',
      val: ''
    },
    roomPrice:{
      label: '房间价格',
      name: 'roomPrice',
      val: 0
    },
    floorIndex:0,
    roomIndex:0
  };
  private priceDifference:any;
  private hotelData;
  private oldPrice;
  private currentRoom:any=[];
  constructor(public navCtrl: NavController, public navParams: NavParams,private alertCtrl: AlertController) {
   this.callBack=this.navParams.get('callBack');
   this.currentRoom=this.navParams.get('currentRoom');
   this.hotelData=this.navParams.get('hotelData');
   this.changeRoomInfo.roomCustomer.val=this.currentRoom.cus;
   this.changeRoomInfo.endTime.val=this.currentRoom.leave;
   this.oldPrice=this.currentRoom.price;
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ChangeRoomPage');
  }

  showRoom() {
    let alert = this.alertCtrl.create();
    alert.setTitle('选择要更换的房间');

    alert.addInput({
      type: 'radio',
      name: 'roomNumber',
      label: '101',
      value: '101',
      checked: false
    });
    alert.addInput({
      type: 'radio',
      name: 'roomNumber',
      label: '103',
      value: '103',
      checked: false
    });

    alert.addButton('取消');
    alert.addButton({
      text: '确认',
      handler: data => {
        this.changeRoomInfo.roomNumber.val=data;
        this.getPriceDifference();
      }
    });
    alert.present();
  }

  // 计算差价
  getPriceDifference(){
    let j,k,thisRoom;
    let changeRoom=this.changeRoomInfo;
    let newPrice;
      for(j=0;j<this.hotelData.length;j++){
        if (changeRoom.roomFloor.val==this.hotelData[j].floor){
          for (k=0;k<this.hotelData[j].rooms.length;k++){
            if (changeRoom.roomNumber.val===this.hotelData[j].rooms[k].name){
              thisRoom=this.hotelData[j].rooms[k];
              newPrice=thisRoom.price;
              this.changeRoomInfo.floorIndex=j;
              this.changeRoomInfo.roomIndex=k;
              break;
            }
          }
        }
      }

    let today=new Date();
    let dateOne=today.getFullYear().toString()+'-'+(today.getMonth()+1).toString()+'-'+today.getDate().toString();
    let dateTwo=changeRoom.endTime.val;
    let leftTime=this.daysBetween(dateOne,dateTwo);
    if(leftTime>0){
      this.changeRoomInfo.roomPrice.val=newPrice;
      this.priceDifference=(newPrice-this.oldPrice)*leftTime;
    }
    else{
      this.changeRoomInfo.roomPrice.val=newPrice;
      this.priceDifference=newPrice-this.oldPrice;
    }
    // else if(leftTime==0){
    //   this.changeRoomInfo.roomPrice.val=newPrice;
    //   this.priceDifference=newPrice-this.oldPrice;
    // }
    // else {
    //   this.priceDifference='客户入住时间不足，不可更换房间';
    // }

  }

  daysBetween(DateOne,DateTwo)
  {
    if(DateOne<DateTwo){
      //原方法为if内部代码，这段代码不分日期1和日期2的顺序，无论谁大谁小都能获得日期差。
      var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
      var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
      var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));

      var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
      var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
      var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));

      var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
      return Math.abs(cha);
    }
    else if(DateOne==DateTwo)
      return 0;
    else
      return -1;
  }

  saveChange() {
    console.info("更换成功");
    this.callBack(this.changeRoomInfo).then(() => {
      this.navCtrl.pop();
    });
  }
}
